import { toast } from "nc-lightapp-front";
// import requestApi from "../requestApi";
import loadData from "./loadData";
import loadOrderData from "./loadOrderData";
import config from "../config";

export default function (props) {
  if(!(props && props.createUIDom)){
    return;
  }
  props.createUIDom(
    {
      pagecode: this.listTabStatus === '0' ? "201102BCLF_FPLC" : config.pagecode, //页面编码
      appcode: this.listTabStatus === '0' ? "201102BCLF" : "201101GRFP", //小应用编码
    },
    (dataui) => {
      let metas = dataui.template;
      console.log("invoice metas:", dataui.template);
      let tempArr = [];
      let headmeta = metas[window.InvoiceWalletAreaCode.billsArea];
      let searchitem = metas[window.InvoiceWalletAreaCode.searchArea];

      searchitem.items.forEach((item) => {
        if (item.itemtype == "refer") {
          item.isShowUnit = true;
        }
        // if (item.attrcode && item.attrcode == "pk_org") {
        //   item.initialvalue = { value: table, display: table };
        // }
        tempArr.push(item);
      });

      //票夹表头开票日期去掉hh:mm:ss
      headmeta.items.forEach(item => {
        if(item.attrcode === 'kprq') {
          item.itemtype = 'datepicker'
        }
      })

      let meta = {
        ...metas,
        searchArea: {
          moduletype: "search",
          items: tempArr,
        },
        head: {
          moduletype: "table",
          items: headmeta.items,
        },
      };
      props.meta.setMeta(meta);
      if(this.props && !this.props.iswallet){
          let areaRight =  document.getElementsByClassName("area-right");
          if(areaRight && areaRight.length > 0 && areaRight[0].style){
              areaRight[0].style.display = "none";
          }
      }
      if(this.listTabStatus === '0'){
        addOperateButtons(this,props,meta,dataui);
        loadData.call(this, props, meta);
      }
      if(this.listTabStatus === '1'){
        loadOrderData.call(this, props, meta);
      }
      // 设置多语=====================开始
      const multiLang = this.props.MutiInit.getIntl(2011);
      let submitExpenseAccount = multiLang.get("submit_expense_account");
      let inReimbursement = multiLang.get("in_reimbursement");
      let haveSubmitExpenseAccount = multiLang.get("have_submit_expense_account");

      let tabData = [];
      tabData.push({ title: submitExpenseAccount, key: '33' });
      tabData.push({ title: inReimbursement, key: '36' });
      tabData.push({ title: haveSubmitExpenseAccount, key: '39' });
      this.setState({tabData});
      // 设置多语=====================结束
    }
  );
}


/**
 * 
 * @param {*} that 组件对象
 * @param {*} headmeta 
 * @param {*} props 
 */
 function addOperateButtons(that,props,meta,dataui) {
      props.button.setButtons(dataui.button);
      const multiLang = props.MutiInit.getIntl(2011); //this.moduleId
      if(!Array.isArray(props.button.getButtons())){
        return ;
      }
      const tableInnerButtons = props.button
            .getButtons()
            .filter((item) => item.area === 'table_inner')
            .map((item) => item.key);
      meta[window.InvoiceWalletAreaCode.billsArea].items.push({
          label: multiLang['201102BCLF-0035'],
          itemtype: 'customer',
          attrcode: 'opr',
          width: '100px',
          visible: true,
          fixed: 'right',
          render: (text, record, index) => {
              const params = {invoiceDataObj: record, invoiceDataIndex: index}
              if(!Array.isArray(tableInnerButtons)){
                return ;
              }
              return props.button.createOprationButton(
                  tableInnerButtons.filter((item) => {
                      return true;
                  }),
                  {
                      area: 'table_inner',
                      buttonLimit: 3,
                      onButtonClick: (props, key) => {
                          if(that.checkInVoice && that.checkInVoice.setImgUrl){
                            that.checkInVoice.setImgUrl({imgBaseUrl: null});
                          }
                          that.setState({checkInvoceIsHidden: false});
                          that.onhandleChangeInvoice(params);
                      }
                  }
              );
          }
      });
}